package ch.ethz.fcl.metrobuzz.algorithm.list;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

public class ListOperation {
	public static <T> List<T> union(List<T> setA, List<T> setB) {
		List<T> tmp = new ArrayList<T>(setA);
		tmp.addAll(setB);
		return tmp;
	}

	public static <T> List<T> unionWithoutDuplicates(List<T> setA, List<T> setB) {
		List<T> tmp = union(setA, setB);
		HashSet<T> hs = new HashSet<T>();
		hs.addAll(tmp);
		tmp.clear();
		tmp.addAll(hs);
		return tmp;
	}

	public static <T> List<T> intersection(List<T> ListA, List<T> setB) {
		List<T> tmp = new ArrayList<T>();
		if (ListA == null || setB == null)
			return tmp;
		for (T x : ListA)
			if (setB.contains(x))
				tmp.add(x);
		return tmp;
	}

	public static <T> List<T> difference(List<T> setA, List<T> setB) {
		List<T> tmp = new ArrayList<T>(setA);
		tmp.removeAll(setB);
		return tmp;
	}

	public static <T> List<T> symUnion(List<T> setA, List<T> setB) {
		List<T> tmpA;
		List<T> tmpB;

		tmpA = union(setA, setB);
		tmpB = intersection(setA, setB);
		return difference(tmpA, tmpB);
	}

	public static <T> boolean isSubset(List<T> setA, List<T> setB) {
		return setB.containsAll(setA);
	}

	public static <T> boolean isSuperset(List<T> setA, List<T> setB) {
		return setA.containsAll(setB);
	}

	public static void main(String args[]) {
		ArrayList<Character> set1 = new ArrayList<Character>();
		ArrayList<Character> set2 = new ArrayList<Character>();

		set1.add('A');
		set1.add('B');
		set1.add('C');
		set1.add('D');

		set2.add('C');
		set2.add('D');
		set2.add('E');
		set2.add('F');

		System.out.println("set1: " + set1);
		System.out.println("set2: " + set2);

		System.out.println("Union: " + union(set1, set2));
		System.out.println("Union without duplicates: " + unionWithoutDuplicates(set1, set2));
		System.out.println("Intersection: " + intersection(set1, set2));
		System.out.println("Difference (set1 - set2): "
				+ difference(set1, set2));
		System.out.println("Symmetric Union: " + symUnion(set1, set2));

		ArrayList<Character> set3 = new ArrayList<Character>(set1);

		set3.remove((char) 'D');
		System.out.println("set3: " + set3);

		System.out.println("Is set1 a subset of set2? " + isSubset(set1, set3));
		System.out.println("Is set1 a superset of set2? "
				+ isSuperset(set1, set3));
		System.out.println("Is set3 a subset of set1? " + isSubset(set3, set1));
		System.out.println("Is set3 a superset of set1? "
				+ isSuperset(set3, set1));

	}
}
